﻿class @BooksView extends CustomView

    initialize: ->
        super
        @setElement("#main")
        @model.bind("change", @render)
        @model.fetch()
    
    events: 
         "click ul.star-rating li a" : "rate"

    rate: (e) ->
        e.preventDefault()
        rating = $(e.currentTarget)
        ul = rating.closest("ul.star-rating:not(.disabled)")

        if ul.length > 0
            ratingModel = new RatingModel()
            ratingModel.set(
                "BookId": ul.data("val-bookid")
                "RatingValue": rating.data("rate-value")
            )

            ratingView = new RatingView(
                model: ratingModel
                el: $(ul)
            )

    render: ->
        $.get(SiteMap.Books.IndexTemplate, (template) =>
            complitedTemplate = _.template(template)
            $(@el).html(complitedTemplate(@model.toJSON()))

            responseModel = new ResponsesModel()
            responseModel.set("BookId", @model.get("BookId"))
            responseView = new ResponsesView({el: $("#responses"), model: responseModel })
        )
